
package com.jf.cloud.service.sysmanager.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.service.spi.ServiceException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jf.cloud.service.sysmanager.common.base.BaseService;
import com.jf.cloud.service.sysmanager.common.db.BaseDao;
import com.jf.cloud.service.sysmanager.common.util.DateUtil;
import com.jf.cloud.service.sysmanager.common.util.ObjectUtil;
import com.jf.cloud.service.sysmanager.dao.IPubgBasePropsConfigService;
import com.jf.cloud.service.sysmanager.entity.PubgBasePropsConfig;
import com.jf.cloud.service.sysmanager.handler.PropConfigHandler;


/**
 * 基础属性配置服务实现
 * 
 * @author 绝峰代码机1.0 创建时间：2018-03-31
 */
@Service("pubgBasePropsConfigService")
public class PubgBasePropsConfigServiceImpl extends BaseService<PubgBasePropsConfig, Long> implements IPubgBasePropsConfigService {

	/**
	 * 日志
	 */
	private static Log log = LogFactory.getLog(PubgBasePropsConfigServiceImpl.class);
	
	/**
	 * JPA公共DAO
	 */
	@Resource
	private BaseDao baseDao;

	/**
	 * 新增 基础属性配置
	 * 
	 * @author 朱建文 创建时间：2018年5月4日 下午2:56:45
	 */
	@Override
	@Transactional
	public PubgBasePropsConfig savePubgBasePropsConfig(PubgBasePropsConfig pubgBasePropsConfig) throws Exception {
		try {
			return this.baseDao.saveAll(pubgBasePropsConfig);
		} catch (Exception e) {
			throw new Exception("新增基础属性配置工出错！", e);
		}
	}
	
	/**
	 * 修改 基础属性配置
	 * 
	 * @author 朱建文 创建时间：2018年5月4日 下午2:56:45
	 */
	@Transactional
	public void updatePubgBasePropsConfig(PubgBasePropsConfig pubgBasePropsConfig) throws Exception {
		try {
			StringBuffer sql = new StringBuffer();
			sql.append(" UPDATE ");
			sql.append("   pubg_base_props_config ");
			sql.append(" SET ");
			sql.append("   CFG_KEY = ?, CFG_VALUE = ?, CFG_TYPE = ?, DEL_FLAG = ?, ");
			sql.append("   REMARK = ?, OPT_ID = ?, OPT_NAME = ?, OPT_TIME = ? ");
			sql.append(" WHERE ");
			sql.append("   ROW_ID = ? ");
			this.baseDao.safeUpdate(sql.toString(), 1, 
					pubgBasePropsConfig.getCfgKey(), pubgBasePropsConfig.getCfgValue(), pubgBasePropsConfig.getCfgType(),
					pubgBasePropsConfig.getDelFlag(), pubgBasePropsConfig.getRemark(), pubgBasePropsConfig.getOptId(), 
					pubgBasePropsConfig.getOptName(), DateUtil.getCurrentDateTime(), pubgBasePropsConfig.getRowId());
		} catch (Exception e) {
			throw new Exception("修改基础属性配置出错！", e);
		}
	}
	
	/**
	 * 更新 配置的删除状态
	 * 
	 * @author 朱建文 创建时间：2018年5月4日 下午2:56:45
	 */
	@Transactional
	public void updateDelFlag(PubgBasePropsConfig pubgBasePropsConfig) throws Exception {
		try {
			StringBuffer sql = new StringBuffer();
			sql.append(" UPDATE ");
			sql.append("   pubg_base_props_config ");
			sql.append(" SET ");
			sql.append("   DEL_FLAG = ?, OPT_ID = ?, OPT_NAME = ?, OPT_TIME = ? ");
			sql.append(" WHERE ");
			sql.append("   ROW_ID = ? ");
			this.baseDao.safeUpdate(sql.toString(), 1, 
					pubgBasePropsConfig.getDelFlag(), pubgBasePropsConfig.getOptId(), pubgBasePropsConfig.getOptName(), 
					DateUtil.getCurrentDateTime(), pubgBasePropsConfig.getRowId());
		} catch (Exception e) {
			throw new Exception("更新 配置的删除状态出错！", e);
		}
	}

	/**
	 * 刷新基础属性配置信息至常量
	 * 
	 * @author 朱建文 创建时间：2018年4月3日 上午11:06:19
	 */
	public void refreshConfigToContant() throws Exception {
		Map<String, Object> cfgData = new HashMap<String, Object>();
		try {
			String sql = " SELECT CFG_KEY, CFG_VALUE FROM pubg_base_props_config WHERE DEL_FLAG = '1' ";
			List<Map<String, Object>> dataList = this.baseDao.findMapList(sql);
			if (dataList != null && !dataList.isEmpty()) {
				for (Map<String, Object> data : dataList) {
					cfgData.put(ObjectUtil.convert(data.get("CFG_KEY")), data.get("CFG_VALUE"));
				}				
			}
			PropConfigHandler.setBasePropConfig(cfgData);
			
			if (PropConfigHandler.getBasePropConfig().containsKey("is_reset_flag")) {
				if ("10".equals(PropConfigHandler.getBasePropConfig("is_reset_flag"))) {
					PropConfigHandler.setBasePropConfig("is_reset_flag", "0");
				}
			}
		} catch (Exception e) {
			throw new ServiceException("刷新基础属性配置信息至常量出错！", e);
		}
	}
	
	/**
	 * 检查配置是否存在
	 * 
	 * @author 朱建文 创建时间：2018年5月4日 下午3:11:12
	 */
	public boolean checkConfigIsExist(PubgBasePropsConfig propsConfig) {
		boolean isExist = true;
		try {
			String sql = " SELECT COUNT(0) FROM pubg_base_props_config WHERE CFG_KEY = ? ";
			if (propsConfig.getRowId() != null && propsConfig.getRowId() > 0) {
				sql += " AND ROW_ID <> " + propsConfig.getRowId();
			}
			isExist = this.baseDao.find(sql, Integer.class, propsConfig.getCfgKey()) >= 1;
		} catch (Exception e) {
			log.error("检查配置是否存在出错！", e);
		}
		return isExist;
	}
}
